package com.kikidao.app.dao;

import com.kikidao.app.exceptions.DAOUncheckedException;

import java.lang.reflect.Field;
import java.sql.Connection;

/**
 * Created with IntelliJ IDEA.
 * User: Georgiana.Scarlat
 * Date: 7/20/12
 * Time: 5:02 PM
 */

public abstract class DAOUtils {


    public abstract Connection getConnection() throws DAOUncheckedException;

    public abstract void endConnection() throws DAOUncheckedException;

    public DAO getDefinedDAO(Class<? extends DAO> daoClass) throws DAOUncheckedException {
        Field fields[] = this.getClass().getDeclaredFields();

        for (Field field : fields) {

            if (field.getType().equals(daoClass)) {

                try {
                    field.setAccessible(true);
                    return (DAO) field.get(this);
                } catch (IllegalAccessException e) {
                    throw new DAOUncheckedException("Can' access DAO " + daoClass, e);
                }
            }
        }
        return null;

    }

}
